// 检查是否登录
function isLogin() {
	if (JSON.parse(localStorage.getItem("is")).isLogin === "true")
		window.location.href = '/index.html';
}

const num = /^\d+$/;
const numCharEN = /^[A-Za-z\d]+$/;
const numCharENCN = /^[A-Za-z\d\u4E00-\u9FA5]+$/;


let usernameResult = false;
let nameResult = false;
let emailResult = false;
let phoneResult = true;//非必需
let passwordResult = false;

// 处理信息
function check() {
	// 获取
	let username = document.getElementById("username").value;
	let name = document.getElementById("name").value;
	let email = document.getElementById("email").value;
	let gender = getGender();
	let phone = document.getElementById("phone").value;
	let password = document.getElementById("password").value;
	let rePassword = document.getElementById("rePassword").value;

	// 检验
	checkUsername(username);
	checkName(name);
	checkEmail(email);
	checkPhone(phone);
	checkPassword(password, rePassword);

	// 储存
	if (usernameResult && nameResult && emailResult && passwordResult) {
		if (phone === "" || phone == null) {
			phone = "";
		} else if (!phoneResult) return false;
		if (gender === "" || gender === "hidden" || gender == null) {
			gender = "hidden";
		}
		setLocalStorage(username, name, email, gender, phone, password);
		alert("注册成功！🥰 即将刷新此页面");
	} else return false;

}


// *检验用户名
function checkUsername(username) {
	if (username === "" || username == null) {
		printError("usernameErr", "用户名不能为空");
	} else if (!(username.length >= 3 && username.length <= 15)) {
		printError("usernameErr", "用户名长度应当在3-15个字符内");
	} else if (!(numCharEN.test(username))) {
		printError("usernameErr", "用户名应当仅由数字或字母组合");
	} else if (localStorage.getItem(username) != null) {
		printError("usernameErr", "当前用户名已被占用");
	} else {
		printError("usernameErr", "");
		usernameResult = true;
	}
}

// *检验昵称
function checkName(name) {
	if (name === "" || name === null) {
		printError("nameErr", "昵称不能为空");
	} else if (!(name.length >= 3 && name.length <= 20)) {
		printError("nameErr", "昵称长度应当在3-20个字符内");
	} else if (!(numCharENCN.test(name))) {
		printError("nameErr", "昵称应当仅由数字、字母或汉字组合");
	} else {
		printError("nameErr", "");
		nameResult = true;
	}
}

// *检验邮箱
function checkEmail(email) {
	if (email === "" || email == null) {
		printError("emailErr", "邮箱不能为空");
	} else if (isEmail(email)) {
		printError("emailErr", "邮箱格式不规范");
	} else if (isOccupied(email)) {
		printError("emailErr", "当前邮箱已被使用");
	} else {
		printError("emailErr", "");
		emailResult = true;
	}

	// 判定邮箱是否有效
	function isEmail(str) {
		// 分析
		let atSymbol = 0;// @
		let dotSymbol = 0;// .
		for (let i = 1; i < str.length; i++)
			if (str.charAt(i) === '@') {
				atSymbol = i;// @的位置
				break;
			}
		for (let i = atSymbol + 1; i < str.length; i++)
			if (str.charAt(i) === '.')
				dotSymbol = i;// .的位置
		// 判定
		if (atSymbol === 0 || atSymbol === str.length - 2) return 1;// 域名
		if (dotSymbol === 0 || dotSymbol === str.length - 1) return 2;
		if (atSymbol < 5 || atSymbol > 20) return 3;// 用户名
		// 结果
		return 0;
	}

	// 检查邮箱是否被使用
	function isOccupied(email) {
		for (let i = 0; i < localStorage.length; i++) {
			let key = localStorage.key(i);
			if (JSON.parse(localStorage.getItem(key)).email === email) {
				return true;
			}
		}
	}
}

// 检验性别
function getGender() {
	let genders = document.getElementsByName("gender");
	for (let i = 0; i < genders.length; i++) {
		if (genders[i].checked) {
			// alert(genders[i].value);
			return genders[i].value;
		}
	}
}

// 检验电话
function checkPhone(phone) {
	if (phone === "" || phone == null) {
	} else if ((!(phone.length >= 4 && phone.length <= 11)) || (!(num.test(phone)))) {
		printError("phoneErr", "非有效电话号码");
		phoneResult = false;
	} else if (isOccupied(phone)) {
		printError("phoneErr", "此电话号码已被占用");
		phoneResult = false;
	} else {
		printError("phoneErr", "");
		phoneResult = true;
	}

	// 检验电话号码是否被占用
	function isOccupied(phone) {
		for (let i = 0; i < localStorage.length; i++) {
			let key = localStorage.key(i);
			if (JSON.parse(localStorage.getItem(key)).phone === phone) {
				return true;
			}
		}
	}
}

// *检验密码
function checkPassword(password, rePassword) {
	if (password === "" || password == null) {
		printError("passwordErr", "密码不能为空");
	} else if (!(password.length >= 6 && password.length <= 20)) {
		printError("passwordErr", "密码长度应当在6-20个字符内");
	} else if (!(numCharEN.test(password))) {
		printError("passwordErr", "密码应当仅由数字或字母组合");
	} else printError("passwordErr", "");

	// 确认密码
	if (rePassword === "" || rePassword == null) {
		printError("rePasswordErr", "确认密码不能为空");
	} else if (rePassword !== password) {
		printError("rePasswordErr", "两次密码不一致");
	} else {
		printError("rePasswordErr", "");
		passwordResult = true;
	}
}


// 储存信息
function setLocalStorage(username, name, email, gender, phone, password) {
	// 存用户
	let account = JSON.stringify({
		"username": username,
		"name": name,
		"email": email,
		"gender": gender,
		"phone": phone,
		"password": password
	});
	localStorage.setItem(username, account);
	// 存登录
	let is = JSON.stringify({"username": username, "isLogin": "false"});
	localStorage.setItem("is", is);
}

// 显示消息
function printError(elemId, hintMsg) {
	let elem = document.getElementById(elemId);
	if (hintMsg === "") {
		elem.innerHTML = "&nbsp;√";
		elem.setAttribute("style", "color: #00a812;");
	} else {
		elem.innerHTML = "× " + hintMsg;
		elem.setAttribute("style", "color: #ee4848;");
	}
}

// 帮助弹窗
function help() {
	alert("\nThere's no help for you. \n\n\n\n\n\n\n\n\n\n\n\n\n" +
		"Because I'm in trouble too.                                                    OK👇");
}

